<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\UserAuth;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class UserAuthSeeder extends Seeder
{
    // php artisan db:seed --class=UserAuthSeeder

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 清空数据表 后在添加
        $userDb = new User();
        $model  = new UserAuth();
        $table  = DB::table('user_auth');
        $table->truncate();

        // 查询前面20个用户 进行认证
        // 1-10个 学校认证
        // 1-10个 机构认证
        $users = $userDb->get();
        $users = json_decode(json_encode($users), true);

        // 执行
        $_end   = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y") + 1);
        $time   = time();
        $number = 1;
        foreach($users as $_user){
            if($number > 20){
                break;
            }

            // 参数
            $_uid  = $_user['id'];
            $_type = $number > 10 ? 'organ' : 'school';   // 学校school，机构organ
            $_row  = $this->makeRow($model, $time, $_end, $_uid, $_type);

            // 保存认证
            $auth_id = DB::table('user_auth')->insertGetId($_row);

            // 改变用户信息
            $_user  = ['auth_name' => $_row['auth_name'], 'auth_type' => $_type, 'is_auth' => 1, 'verify' => 1, 'auth_time' => $time, 'end_time' => $_end, 'auth_id' => $auth_id];
            $result = $userDb->where(['id' => $_uid])->update($_user);

            $number++;
        }


    }

    // 生成数据
    protected function makeRow($model, $time, $_end, $uid, $_type)
    {
        // 幼儿园garten，小学 little，初高中 middle
        $grades = ['garten', 'little', 'middle'];
        $grade  = '';
        if($_type == 'school'){
            $grade = $grades[mt_rand(0, 2)];
        }

        // 参数
        $nums   = mt_rand(3, 7);
        $d_nums = mt_rand(30, 70);
        $_row   = [
            'uid'          => $uid,
            'auth_type'    => $_type,
            'auth_name'    => getChar($nums).($_type == 'school' ? '学校' : '机构'),
            'order_no'     => $model->makeOrderNo($uid),
            'grade'        => $grade,
            'description'  => getChar($d_nums),
            'city'         => '成都市',
            'address'      => getChar($nums).'路'.$d_nums.'号',
            'telephone'    => '028-'.mt_rand(10000000, 99999999),
            'license_img'  => 'https://img.shenlanhui.com/static/head_img/face_'.rand(1, 450).'.jpg',
            'school_allow' => 'https://img.shenlanhui.com/static/head_img/face_'.rand(1, 450).'.jpg',
            'is_pay'       => 1,
            'verify'       => 1,
            'times'        => 1,
            'is_propose'   => 1,
            'form_time'    => $time,
            'end_time'     => $_end,
            'ctime'        => $time,
            'mtime'        => $time,
        ];

        return $_row;
    }


}
